<template>
  <TopMenu/>
  <div class="container flex-content">
    <div class="left-side">
      <div class="left-side-item" :class="currentType == index + 1 ? 'active' : ''"
           v-for="(item, index) in categoryTabs"
           @click="changeTab(index)"
           :key="`category_${index}`">{{item.name}}</div>
    </div>
    <div class="eventContainer" v-if="!currentDetails">
      <!--   公告   -->
      <section class="postItems" v-if="currentType == 1"
               v-for="(p,idx) in currentData"
               :key="`post_${idx}`"
               @click="seeDetailContent(p.content)">
        <div>
          <p class="post-title">{{ p.title }}</p>
          <p class="createDate">{{ p.createDate }}</p>
        </div>
        <div class="see-detail">查看详情 ></div>
      </section>
      <!--   新闻   -->
      <section class="newItems" v-if="currentType == 2">
        <div class="newsBox"
             v-for="(news ,index) in currentData"
             :key="`news_${index}`"
             @click="seeDetailContent(news.content)">
          <img :src="news.image" :alt="news.title" />
          <div class="infos">
            <p class="_title">{{news.title}}</p>
            <p class="_date">{{news.createDate}}</p>
          </div>
          <div class="_seeDetail">查看详情 ></div>
        </div>
      </section>
      <!--   图集   -->
      <section class="albumItems" v-if="currentType == 3">
        <img
          v-for="(image, index) in currentData"
          :key="index"
          :src="image.image"
          @click="openPreview(index)"
          :alt="image.name"
          style=""
        />

        <!-- 图片预览组件 -->
        <ImagePreview
          v-model:visible="previewVisible"
          v-model:current-index="currentIndex"
          :images="albumList"
          :infinite="true"
          :show-thumbnails="true"
        />
      </section>
    </div>
    <div class="details-wrap" v-if="currentDetails">
      <p class="toBack" @click="currentDetails = null"> 返回上一级</p>
      <div class="detail-content" v-html="currentDetails"></div>
    </div>
  </div>
</template>
<script setup>
import TopMenu from '@/components/TopMenu/index.vue'
import { useRoute } from 'vue-router'
import { getByMore } from '@/api/data.js'
import { useEventStore } from '@/stores/event/index.js'
import ImagePreview from '@/components/common/ImagePreview/index.vue'
const $route = useRoute()
const eventStore = useEventStore()

const previewVisible = ref(false)
const currentIndex = ref(0)
const currentType = ref($route.query.type || 1)
const currentDataId = ref($route.query.id || null)
const categoryTabs = ref([
  {name: "公告", value: 1},
  {name: "新闻资讯", value: 2},
  {name: "精彩图集", value: 3},
])
const albumList = ref([])
const currentData = ref([])
const currentDetails = ref(null)
const openPreview = (index) => {
  currentIndex.value = index
  previewVisible.value = true
}
const seeDetailContent = (content) => {
  currentDetails.value = content;
}
const getDetails = ()=> {
  const params = {
    id: eventStore.eventData.id,
    type: currentType.value,
  }
  getByMore(params).then(res => {
    if (res.code == 200) {
      currentData.value = res.data;
      if (currentDataId.value) {
        currentDetails.value = res.data.filter(item => item.id == currentDataId.value)[0].content
      }
      if (currentType.value == 3) {
        albumList.value = res.data.map(item =>{
          return item.image;
        })
      }
    }
  })
}
const changeTab =(index)=> {
  currentDetails.value = null
  currentType.value = index + 1;
  getDetails()
}
onMounted(() => {
  getDetails()
})
</script>

<style scoped lang="scss">
.flex-content {
  display: flex;
  justify-content: space-between;
  gap: 30px;
  margin-top: 30px;
}

.eventContainer {
  min-height: 100px;
  display: flex;
  flex-direction: column;
  gap: 20px;
  flex: 1;
  padding-bottom: 30px;
  .postItems {
    background: #ffffff;
    border-radius: 12px;
    padding: 24px 16px;
    flex: 1;
    display: flex;
    align-items: center;
    gap: 12px;
    border: 1px solid #e8e8e8;
    cursor: pointer;
    .post-title {
      font-size: 18px;
      font-weight: bold;
    }
    &:hover {
      //border-color: #5F3789;
      color: #5F3789;
      box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
      transition: transform 0.5s cubic-bezier(0.175, 0.885, 0.32, 1.275),
      box-shadow 0.5s ease,
      filter 0.5s ease;
    }
    .createDate {
      font-size: 14px;
      padding-top: 10px;
      color: #bbb;
    }
    .see-detail {
      color: #5F3789;
      margin-left: auto;
    }
  }
  .albumItems {
    display: flex;
    flex-wrap: wrap;
    gap: 20px;
    img {
      width: 195px;
      height: 130px;
      object-fit: cover;
      cursor: pointer;
      border-radius: 8px;
      &:hover {
        transform: scale(1.15);
        box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
        transition: transform 0.5s cubic-bezier(0.175, 0.885, 0.32, 1.275),
        box-shadow 0.5s ease,
        filter 0.5s ease;
      }
    }
  }

  .newItems {
    display: flex;
    flex-direction: column;
    gap: 30px;
  }
  .newsBox {
    flex: 1;
    border-radius: 8px;
    border: 1px solid #e8e8e8;
    display: flex;
    align-items: center;
    padding-right: 20px;
    &:hover {
      box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
      transition: transform 0.5s cubic-bezier(0.175, 0.885, 0.32, 1.275),
      box-shadow 0.5s ease,
      filter 0.5s ease;
      transform: scale(1.01);
    }
    .infos {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      height: 100%;
      padding: 12px;
      cursor: pointer;
      ._title {
        font-size: 18px;
      }
      ._date {
        color: #9b9b9b;
      }
    }
    img {
      border-radius: 8px 0 0 8px;
      width: 230px;
      height: 140px;
      object-fit: cover;
    }
    ._seeDetail {
      margin-left: auto;
      cursor: pointer;
      color: #5F3789;
    }
  }
}
.details-wrap {
  flex: 1;
  .toBack {
    font-size: 16px;
    font-weight: bold;
    border-bottom: 1px solid #e8e8e8;
    padding-bottom: 20px;
    cursor: pointer;
    color: #6b46c1;
    &:hover  {
      color: #5F3789;
      transform: scale(1.01);
    }
  }
  .detail-content {
    padding: 24px 12px;
  }
}
</style>
